<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>成绩管理</el-breadcrumb-item>
      <el-breadcrumb-item>学生成绩</el-breadcrumb-item>
    </el-breadcrumb>
    <el-row :gutter="10">
      <!--搜索框-->
      <el-col :span="6">
        <el-input placeholder="按学号查询" v-model="searchNum" class="input-with-select" @keyup.enter.native="searchData1">
          <el-button slot="append" icon="el-icon-search" @click="searchdata1"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6" style="margin-left: 90px;margin-right: 90px;">
        <el-input placeholder="按课程名称查询" v-model="search" class="input-with-select" @keyup.enter.native="searchData2">
          <el-button slot="append" icon="el-icon-search" @click="searchdata"></el-button>
        </el-input>
      </el-col>
      <el-col :span="6">
        <el-input placeholder="按课程性质查询" v-model="searchNature" class="input-with-select"
                  @keyup.enter.native="searchData3">
          <el-button slot="append" icon="el-icon-search" @click="searchdata2"></el-button>
        </el-input>
      </el-col>
    </el-row>

    <!--表单模块-->
    <el-table :data="grades" highlight-current-row
              ref='table' border stripe height="300" style="margin-top: 30px;">
      <el-table-column type="index" fixed align="center"></el-table-column>
      <el-table-column prop="gradeId" label="成绩号"  align="center"></el-table-column>
      <el-table-column prop="gradeYear" label="学年" align="center" width="130"></el-table-column>
      <el-table-column prop="gradeTerm" label="学期" align="center"></el-table-column>
      <el-table-column prop="stuNum" label="学号" align="center" width="120"></el-table-column>
      <el-table-column prop="couId" label="课程号" align="center" width="120"></el-table-column>
      <el-table-column prop="couName" label="课程名称" align="center" width="150"></el-table-column>
      <el-table-column prop="couNature" label="课程性质" align="center"></el-table-column>
      <el-table-column prop="couCredit" label="学分" align="center"></el-table-column>
      <el-table-column prop="gradePoint" label="绩点" align="center"></el-table-column>
      <el-table-column prop="grade" label="成绩" align="center"></el-table-column>
      <el-table-column prop="couCollege" label="开课学院" align="center" width="150"></el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="120">
        <template slot-scope="scope">
          <!-- 修改 -->
          <el-tag
            type="primary"
            effect="dark"
            @click="openupdate(scope.row)"
          >编辑
          </el-tag>
          <el-tag
            type="danger"
            effect="dark"
            @click="deleteGrade(scope.row.gradeId)"
          >删除
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!--分页插件-->
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curpage"
      :page-sizes="[2, 4, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
    <!-- 编辑 -->
    <el-dialog title="编辑成绩" :visible.sync="updategradedialog" @close="closeupdate">
      <el-form :model="updategradeform"  ref="updateform" label-width="auto"  style="width:93%"
               class="demo-form-inline form">
        <el-form-item  label="成绩号" >
          <el-input v-model="updategradeform.gradeId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="学年" >
          <el-input v-model="updategradeform.gradeYear" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="学期" >
          <el-input v-model="updategradeform.gradeTerm" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="学号" >
          <el-input v-model="updategradeform.stuNum" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程号" >
          <el-input v-model="updategradeform.couId" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程名称" >
          <el-input v-model="updategradeform.couName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="课程性质" >
          <el-select v-model="updategradeform.couNature"  filterable style="width: 100%;">
            <el-option label="必修课" value="必修课"></el-option>
            <el-option label="通识任选课" value="通识任选课"></el-option>
            <el-option label="选修课" value="选修课"></el-option>
            <el-option label="必修项目" value="必修项目"></el-option>
            <el-option label="英语拓展课" value="英语拓展课"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="学分" >
          <el-input v-model="updategradeform.couCredit" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="绩点" >
          <el-input v-model="updategradeform.gradePoint" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="成绩" >
          <el-input v-model="updategradeform.grade" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item  label="开课学院" >
          <el-input v-model="updategradeform.couCollege" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updategradedialog = false">取 消</el-button>
        <el-button type="primary" @click="updateGrade">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
  import axios from 'axios';//axios
  import Qs from 'qs';//qs
  export default {
    name:"gradeList",
    //绑定数据
    data() {
      return {
        grades:[],//注意使用表格时，pop属性对应的内容等价于后端数据传输的字段(键)
        curpage:1,//当前页
        pagesize:4,//每页显示条数
        total:0,//总条数
        updategradedialog:false,
        updategradeform:{//修改功能数据绑定
          gradeId:"",
          gradeYear:"",
          gradeTerm:"",
          stuNum:"",
          couId:"",
          couName:"",
          couCredit:"",
          couNature:"",
          gradePoint:"",
          grade:"",
          couCollege:""
        },
        search:"",//模糊查询的内容
        searchNum:"",
        searchNature:""
      }
    },
    //文档加载事件,当没有任何按钮去点击触发事件时，可以使用该函数在加载时，触发事件！
    mounted() {
      this.gradeList();
    },
    methods:{
      selectRow(row) { // 给操作行 高亮
        if (row) {
          this.$refs.table.setCurrentRow(row, true);  // 高亮某一行
        } else {
          this.$refs.table.setCurrentRow();  // 取消高亮
        }
      },
      //执行分页查询的函数
      gradeList(curpage = 1) {
        var grades=this;
        axios.get(
          "http://localhost:1313/grade/selectGradeCouNameByAdmin",
          {params:{current:grades.curpage, rows: grades.pagesize,search:grades.search}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.grades = res.data.data.grades;
          this.total = res.data.data.total;
        });
      },
      // 监听 pageSize 改变的事件
      handleSizeChange (newSize) {
        //   console.log(newSize)
        //  将监听接受到的每页显示多少条的数据 newSzie 赋值给 pagesize
        this.pagesize = newSize;
        //  修改完以后，重新发起请求获取一次数据
        this.gradeList();
      },
      // 监听 页码值  改变的事件
      handleCurrentChange (newPage) {
        //   console.log(newPage)
        //  将监听接受到的页码值的数据 newPage 赋值给 pagenum
        this.curpage = newPage;
        //  修改完以后，重新发起请求获取一次数据
        this.gradeList();
      },
      //关闭添加选课的模态框
      clearuseradd() {
        // 重置表单内容
        this.$refs.addform.resetFields();
      },
      openupdate(data){
        console.log(data)
        //打开模态框
        this.updategradedialog=true;
        //数据回显
        this.updategradeform = data;
      },
      //修改成绩
      updateGrade() {
        var updateInfo = this;
        axios({
          headers:{'Content-Type':'application/x-www-form-urlencoded'},
          method:"post",
          url:"http://localhost:1313/grade/gradeUpdate",
          data:Qs.stringify(updateInfo.updategradeform)
        }).then((res)=>{
          if (res.data.code == 200) {
            this.$message({
              message:res.data.msg,
              type:"success"
            });
            this.updategradedialog = false;
            this.gradeList();
          }else{
            this.$message.error(res.data.msg)
          }
        })
      },
      //关闭修改的模态框
      closeupdate() {
        this.$refs.updateform.resetFields();
      },
      deleteGrade(data){
        console.log(data)
        //询问窗口
        this.$confirm("是否删除该成绩？","提示",{
          confirmButtonText:"确定",
          cancelButtonText:"取消",
          type:"warning",
        }).then(()=>{
          //发送请求到后端
          axios.get(
            "http://localhost:1313/grade/gradeDelete",
            {params:{gradeId:data}}
          ).then((res)=>{
            if (res.data.code == 200) {
              this.$message({
                type:"success",
                message:res.data.msg
              });
              this.curpage = 1;//重新设置当前页面
              this.gradeList();//刷新页面
            }else{
              this.$message.error(res.data.msg);
            }
          })
        }).catch(()=>{
          this.$message({
            type:"info",
            message:"已取消删除"
          })
        })
      },
      //课程名称的模糊查询
      searchdata(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        this.gradeList();
        this.search ="";
      },
      // 根据学号完成查询
      gradeList1(){
        var grades=this;
        axios.get(
          "http://localhost:1313/grade/queryGradeByStuNumPage",
          {params:{current:grades.curpage, rows: grades.pagesize,search:grades.searchNum}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.grades = res.data.data.grades;
          this.total = res.data.data.total;
        });
      },
      // 根据课程性质完成查询
      gradeList2(){
        var grades=this;
        axios.get(
          "http://localhost:1313/grade/selectGradeCouNatureByAdmin",
          {params:{current:grades.curpage, rows: grades.pagesize,search:grades.searchNature}}
        ).then((res)=>{
          console.log(res)
          //将返回数据赋值给绑定数据，完成数据渲染
          this.grades = res.data.data.grades;
          this.total = res.data.data.total;
        });
      },
      searchdata1(){
        this.curpage = 1;
        this.gradeList1();
        this.searchNum ="";
      },
      searchdata2(){
        this.curpage = 1;
        //回调分页查询，增加分页查询的参数
        this.gradeList2();
        this.searchNature ="";
      },
      // 按下回车键实现搜索
      searchData1(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata1();
        }
      },
      // 按下回车键实现搜索
      searchData2(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.curpage = 1;
          //回调分页查询，增加分页查询的参数
          this.gradeList();
          this.search ="";
        }
      },
      // 按下回车键实现搜索
      searchData3(e){
        var keyCode = window.event? e.keyCode:e.which;
        console.log("回车搜索", keyCode, e);
        if (keyCode == 13) {
          this.searchdata2();
        }
      }
    }
  }
</script>

<style>
  /* 用来设置当前页面element全局table 选中某行时的背景色*/
  .el-table__body tr.current-row>td{
    background-color:  rgba(227, 189, 241, 0.76) !important;
    color: #fff;
  }
  .el-input--suffix .el-input__inner {
    padding-right: 15px;
  }
  .form{
    display: grid;
    justify-content: center;
    /* align-items: center; */
    justify-items: center;
  }
  .el-button--info.is-plain {
    color: #704141;
    background: #f4f4f5;
    border-color: #d3d4d6;
  }
  .el-button--mini {
    font-size: 14px;
    border-radius: 3px;
  }
  .el-breadcrumb{
    padding-bottom: 20px;
  }
  .el-pagination{
    padding-top: 20px;
  }
</style>
